//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BjerksundStenslandApproximationEngine.h"
using namespace Cephei::QL::Pricingengines::Vanilla;
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
using namespace Cephei::QL::Processes;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Vanilla::CBjerksundStenslandApproximationEngine::CBjerksundStenslandApproximationEngine (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CGeneralizedBlackScholesProcess^ _Cprocess;
    try
    {
#ifdef HANDLE
        _phBjerksundStenslandApproximationEngine = NULL;
#endif
        _Cprocess = safe_cast<CGeneralizedBlackScholesProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& _process = static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>&> (_Cprocess->GetShared ()); 
        _ppBjerksundStenslandApproximationEngine = new boost::shared_ptr<QuantLib::BjerksundStenslandApproximationEngine> (new QuantLib::BjerksundStenslandApproximationEngine ( _process ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Cephei::QL::Pricingengines::Vanilla::CBjerksundStenslandApproximationEngine::CBjerksundStenslandApproximationEngine (boost::shared_ptr<QuantLib::BjerksundStenslandApproximationEngine>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBjerksundStenslandApproximationEngine = NULL;
#endif
	_ppBjerksundStenslandApproximationEngine = &childNative;
    
}
Cephei::QL::Pricingengines::Vanilla::CBjerksundStenslandApproximationEngine::CBjerksundStenslandApproximationEngine (QuantLib::BjerksundStenslandApproximationEngine& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phBjerksundStenslandApproximationEngine = NULL;
#endif
	_ppBjerksundStenslandApproximationEngine = new boost::shared_ptr<QuantLib::BjerksundStenslandApproximationEngine> (&childNative);
    
    _BjerksundStenslandApproximationEngineOwner = owner;
    
}

Cephei::QL::Pricingengines::Vanilla::CBjerksundStenslandApproximationEngine::CBjerksundStenslandApproximationEngine (CBjerksundStenslandApproximationEngine^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBjerksundStenslandApproximationEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBjerksundStenslandApproximationEngine = new boost::shared_ptr<QuantLib::BjerksundStenslandApproximationEngine> (copy->GetShared());
        
    }
}
Cephei::QL::Pricingengines::Vanilla::CBjerksundStenslandApproximationEngine::CBjerksundStenslandApproximationEngine (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBjerksundStenslandApproximationEngine = NULL;
#endif
	if (!t->IsSubclassOf(CBjerksundStenslandApproximationEngine::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Vanilla::CBjerksundStenslandApproximationEngine::CBjerksundStenslandApproximationEngine (QuantLib::Handle<QuantLib::BjerksundStenslandApproximationEngine>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBjerksundStenslandApproximationEngine = &childNative;
	_ppBjerksundStenslandApproximationEngine = &static_cast<boost::shared_ptr<QuantLib::BjerksundStenslandApproximationEngine>>(childNative.currentLink());
    
    _BjerksundStenslandApproximationEngineOwner = owner;
}
Cephei::QL::Pricingengines::Vanilla::CBjerksundStenslandApproximationEngine::CBjerksundStenslandApproximationEngine (QuantLib::Handle<QuantLib::BjerksundStenslandApproximationEngine> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBjerksundStenslandApproximationEngine = &childNative;
	_ppBjerksundStenslandApproximationEngine = &static_cast<boost::shared_ptr<QuantLib::BjerksundStenslandApproximationEngine>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Vanilla::CBjerksundStenslandApproximationEngine::CBjerksundStenslandApproximationEngine (QuantLib::BjerksundStenslandApproximationEngine childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBjerksundStenslandApproximationEngine = NULL;
#endif
	_ppBjerksundStenslandApproximationEngine = new boost::shared_ptr<QuantLib::BjerksundStenslandApproximationEngine> (new QuantLib::BjerksundStenslandApproximationEngine (childNative));
    
}
#endif

Cephei::QL::Pricingengines::Vanilla::CBjerksundStenslandApproximationEngine::~CBjerksundStenslandApproximationEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppBjerksundStenslandApproximationEngine != NULL)
    {
	    delete _ppBjerksundStenslandApproximationEngine;
        _ppBjerksundStenslandApproximationEngine = NULL;
    }
}
Cephei::QL::Pricingengines::Vanilla::CBjerksundStenslandApproximationEngine::!CBjerksundStenslandApproximationEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppBjerksundStenslandApproximationEngine != NULL)
    {
	    delete _ppBjerksundStenslandApproximationEngine;
    }
}
QuantLib::BjerksundStenslandApproximationEngine& Cephei::QL::Pricingengines::Vanilla::CBjerksundStenslandApproximationEngine::GetReference ()
{
    if (_ppBjerksundStenslandApproximationEngine == NULL) throw gcnew NativeNullException ();
	return **_ppBjerksundStenslandApproximationEngine;
}
boost::shared_ptr<QuantLib::BjerksundStenslandApproximationEngine>& Cephei::QL::Pricingengines::Vanilla::CBjerksundStenslandApproximationEngine::GetShared ()
{
    if (_ppBjerksundStenslandApproximationEngine == NULL) throw gcnew NativeNullException ();
	return *_ppBjerksundStenslandApproximationEngine;
}
QuantLib::BjerksundStenslandApproximationEngine* Cephei::QL::Pricingengines::Vanilla::CBjerksundStenslandApproximationEngine::GetPointer ()
{
    if (_ppBjerksundStenslandApproximationEngine == NULL) throw gcnew NativeNullException ();
	return &**_ppBjerksundStenslandApproximationEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BjerksundStenslandApproximationEngine>& Cephei::QL::Pricingengines::Vanilla::CBjerksundStenslandApproximationEngine::GetHandle ()
{
	if (_phBjerksundStenslandApproximationEngine == NULL)
	{
		_phBjerksundStenslandApproximationEngine = new Handle<QuantLib::BjerksundStenslandApproximationEngine> (*_ppBjerksundStenslandApproximationEngine);
	}
	return *_phBjerksundStenslandApproximationEngine;
}
#endif
bool Cephei::QL::Pricingengines::Vanilla::CBjerksundStenslandApproximationEngine::HasNative () 
{
	return (_ppBjerksundStenslandApproximationEngine != NULL);
}

Cephei::QL::Pricingengines::Vanilla::IBjerksundStenslandApproximationEngine^ Cephei::QL::Pricingengines::Vanilla::CBjerksundStenslandApproximationEngine::Calculate::get ()
{
    try
    {
    	(*_ppBjerksundStenslandApproximationEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Vanilla::IBjerksundStenslandApproximationEngine^ Cephei::QL::Pricingengines::Vanilla::CBjerksundStenslandApproximationEngine_Factory::Create (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process)
{
    return gcnew CBjerksundStenslandApproximationEngine ( process);
}
